package com.wemei.auto.ui.utils

//import com.yanzhenjie.album.AlbumFile
//import com.yanzhenjie.album.AlbumLoader
import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.wemei.auto.ui.ext.dp
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


object GlideUtils {
    /**
     * 加载圆形
     */
    fun intoCircle(tag: Any, url: Any?, imageView: ImageView, placeholder: Int? = null) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            requestBuilder.apply(RequestOptions.circleCropTransform())
            requestBuilder.into(imageView)
        }
    }
    // Glide.with(ivImage.context).load(bean.filePath ?: bean.url ?: bean.resId).apply(RequestOptions().transform(CenterCrop(), RoundedCorners(SizeUtils.dp2px(10f)))).into(ivImage)
    /**
     * 加载圆角
     */
    fun intoRoundedCorners(
        tag: Any,
        url: Any?,
        imageView: ImageView,
        placeholder: Int? = null,
        roundingRadius: Int? = null,
    ) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            if (roundingRadius != null) requestBuilder.apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundedCorners(roundingRadius.dp)
                )
            )
            requestBuilder.into(imageView)
        }
    }

    /**
     * 加载圆角,设置那个方向需要圆角
     */
    fun intoRoundedCorners(
        tag: Any,
        url: Any?,
        imageView: ImageView,
        placeholder: Int? = null,
        roundingRadius: Int? = 0,
        cornerType: RoundedCornersTransformation.CornerType,
    ) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (placeholder != null) requestBuilder.placeholder(placeholder).error(placeholder)
            if (roundingRadius != null) requestBuilder.apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundedCornersTransformation(roundingRadius.dp, 0, cornerType)
                )
            )
            requestBuilder.into(imageView)
        }
    }

    fun intoView(
        tag: Any,
        url: Any?,
        imageView: ImageView,
        requestOptions: RequestOptions? = null,
        placeholder: Int? = null,
        error: Int? = null,
    ) {
        if (url == null) return
        withRequestManager(tag)?.run {
            val requestBuilder = load(url)
            if (requestOptions != null)
                requestBuilder.apply(requestOptions)
            if (placeholder != null) requestBuilder.placeholder(placeholder)
            if (error != null) requestBuilder.error(error)
            requestBuilder.into(imageView)
        }
    }

    private fun withRequestManager(tag: Any): RequestManager? {
        return when (tag) {
            is View -> Glide.with(tag)
            is Context -> Glide.with(tag)
            is Activity -> Glide.with(tag)
            is androidx.fragment.app.Fragment -> Glide.with(tag)
            is FragmentActivity -> Glide.with(tag)
            is Fragment -> Glide.with(tag)
            else -> {
                Logger.e("tag不合法")
                null
            }
        }

    }
}